﻿using System.Collections.Generic;
using Cms.Application.Admin.Views;
using Cms.Application.IO;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class EditStylesheetPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly IEditStylesheetView _view;
        public IDiskAccess DiskAccess;

        public EditStylesheetPresenter(IEditStylesheetView view)
            : this(view, new RepositoryFactory())
        {
            DiskAccess = new DiskAccess();
        }
        
        public EditStylesheetPresenter(IEditStylesheetView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.EditStylesheet)
                _view.RedirectToPermissionDeniedPage();
        }

        public void SetContexts()
        {
            IDictionary<StylesheetContext, string> contexts = new Dictionary<StylesheetContext, string>
                                                                   {
                                                                       {
                                                                           StylesheetContext.Always,
                                                                           GetText(_view, "ContextAlways")
                                                                           },
                                                                       {
                                                                           StylesheetContext.Print,
                                                                           GetText(_view, "ContextPrint")
                                                                           },
                                                                       {
                                                                           StylesheetContext.Editor,
                                                                           GetText(_view, "ContextEditor")
                                                                           },
                                                                       {
                                                                           StylesheetContext.PublicAsAdmin,
                                                                           GetText(_view, "ContextPublicAsAdmin")
                                                                           }
                                                                   };
            _view.SetContexts(contexts);
        }

        public void Initialize()
        {
            if (_view.StylesheetID > 0)
            {
                Stylesheet stylesheet = SiteRepository.GetStylesheet(_view.StylesheetID);
                _view.Name = stylesheet.Name;
                _view.SelectedContext = stylesheet.Context;
                _view.SourceCode = stylesheet.GetCode();
            }
        }

        public void Save()
        {
            if (_view.Name.Trim() == "")
                AddValidationError(GetText(_view, "NoNameError"));
            if (!_administrator.CurrentSite.CanStylesheetBeNamedTo(_view.StylesheetID, _view.Name))
                AddValidationError(GetText(_view, "NameCollisionError"));
            if (_view.SourceCode.Trim() == "")
                AddValidationError(GetText(_view, "NoSourceCodeError"));
            if (!IsValid)
                return;
            Stylesheet stylesheet;
            if (_view.StylesheetID == -1)
            {
                stylesheet = new Stylesheet();
                stylesheet.Site = _administrator.CurrentSite;
                stylesheet.Site.Stylesheets.Add(stylesheet);
            }
            else
            {
                stylesheet = SiteRepository.GetStylesheet(_view.StylesheetID);
            }
            stylesheet.Name = _view.Name;
            stylesheet.Context = _view.SelectedContext;
            stylesheet.SetCode(_view.SourceCode, _administrator);
            SiteRepository.SaveSite(_administrator.CurrentSite);
            DiskAccess.DeleteFile("__System\\Stylesheets\\" + stylesheet.ID + ".css");
            DiskAccess.CreateDirectory("__System\\Stylesheets");
            DiskAccess.CreateTextFile("__System\\Stylesheets\\" + stylesheet.ID + ".css", _view.SourceCode);
            _view.SetStylesheetID(stylesheet.ID);
        }
    }
}